package f.h.dragon.patterns.gateway;

import junit.framework.Assert;

/**
 * MessageGateway TODO
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/04/23
 * @since 1.0
 */
public class MessageGateway {
	
	//local variables
	/**
	 * confirmation flag
	 */
	protected static final String CONFIRM = "Confirm";
	/**
	 * message sender
	 */
	private MessageSender messageSender;
	
	//Logic
	/**
	 * send confirmation message
	 * 
	 * @param orderID
	 * @param amount
	 * @param bymbol
	 */
	public void sendConfirmation(String orderID, int amount, String symbol){
		Object[] args = {orderID, amount, symbol};
		send(CONFIRM, args);
	}

	/**
	 * send message
	 * 
	 * @param msgType
	 * @param args
	 */
	private void send(String msgType, Object[] args) {
		//send
		int returnCode = doSend(msgType, args);
		// null args
		if(returnCode == MessageSender.NULL_PARAMETER){
			throw new NullPointerException("Null Parameter passed for msg type: " + msgType);
		}
		//success
		if(returnCode != MessageSender.SUCCESS){
			throw new IllegalStateException("Unexpected error from messaging system #:" + returnCode);
		}
	}

	/**
	 * send msg by messageSender
	 * 
	 * @param msgType
	 * @param args
	 * @return
	 */
	protected int doSend(String msgType, Object[] args) {
		Assert.assertNotNull(messageSender);
		return messageSender.send(msgType, args);
	}

}
